//
//  NewsCommentViewController.m
//  GoodBuy
//
//  Created by Cyber on 15/5/23.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "NewsCommentViewController.h"
#import "ChinaCommentViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "CommentChinaTableViewCell.h"
#import "ChinaCommentlei.h"
#import "MJRefresh.h"

@interface NewsCommentViewController ()
@property (nonatomic, retain) UITableView *tv;
@property (nonatomic, retain) NSMutableArray *ArrComment;

@end

@implementation NewsCommentViewController

- (void)dealloc
{
    [_webview release];
    [_juhua release];
    [_ArrComment release];
    [_tv release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"ta 在说";
    //    self.navigationController.navigationBar.translucent = YES;
    [self createTableView];
    [self xiaojuhua];
    [self addHeader];
    [self addFooter];
}
#warning mark - 下拉刷新
- (void)addHeader
{
    [self.tv addHeaderWithTarget:self action:@selector(headerRefreshing)];
}
- (void)headerRefreshing
{
    [self getData];
}
#warning mark - 上拉加载
- (void)addFooter
{
    [self.tv addFooterWithTarget:self action:@selector(footerRefreshing)];
}
- (void)footerRefreshing
{
    [self getRedata];
}
#pragma mark - 菊花
- (void)xiaojuhua
{
    self.juhua = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:_juhua];
    _juhua.labelText = @"亲 请稍等";
    _juhua.tintColor = [UIColor grayColor];
    [self.juhua show:YES];
    [_juhua release];
    [self getData];
    
}
// ---------------判断无评论时弹出框
- (void)NoneComment
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"暂无评论,抢沙发哦" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [self.view addSubview:alert];
    [alert show];
    [alert release];
}
// --------------获取数据方法
- (void)getData
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/comments?limit=20&article_id=%@&type=news", self.news.article_id];
    NSLog(@"0+++++++ %@", url_string);
    //    NSLog(@"!!!!%@", url_string);
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id dic1 = responseObject;
        id dic2 = [dic1 objectForKey:@"data"];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic2 objectForKey:@"rows"];
        self.ArrComment = [[NSMutableArray alloc]init];
        //        NSLog(@"!!!%@", arr);
        for (NSDictionary *dic in arr) {
            ChinaCommentlei *China = [[ChinaCommentlei alloc]init];
            [China setValuesForKeysWithDictionary:dic];
            [_ArrComment addObject:China];
        }
        if (_ArrComment.count == 0) {
            [self NoneComment];
            [self.juhua hide:YES afterDelay:1];
        }else{
            //        NSLog(@"~~~%@", _ArrComment);
            [_tv reloadData];
            [self.tv headerEndRefreshing];
            [self.juhua hide:YES afterDelay:1];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败==== %@",error);
    }];
}
- (void)getRedata
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://api.smzdm.com/v1/comments?offset=%ld&limit=20&article_id=%@&type=news", _limitNumber+20, self.news.article_id];
    self.limitNumber = self.limitNumber + 20;
        NSLog(@"~!!~~ %@", url_string);
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        id dic1 = responseObject;
        id dic2 = [dic1 objectForKey:@"data"];
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        arr = [dic2 objectForKey:@"rows"];
        //        self.ArrComment = [[NSMutableArray alloc]init];
        
        for (NSDictionary *dic in arr) {
            ChinaCommentlei *China = [[ChinaCommentlei alloc]init];
            [China setValuesForKeysWithDictionary:dic];
            [_ArrComment addObject:China];
        }
        //        NSLog(@"~~~%@", _ArrComment);
        [_tv reloadData];
        [self.tv footerEndRefreshing];
        [self.juhua hide:YES afterDelay:1];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败==== %@",error);
    }];
    
}
//------------------------------------


- (void)createTableView
{
    self.tv = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tv.delegate = self;
    _tv.dataSource = self;
    
    _tv.rowHeight = [UIScreen mainScreen].bounds.size.height / 4 + 40;
    [self.view addSubview:_tv];
    [_tv release];
    [_tv registerClass:[CommentChinaTableViewCell class] forCellReuseIdentifier:@"reuse"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentChinaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.comment = _ArrComment[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _ArrComment.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [UIScreen mainScreen].bounds.size.height / 3;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
